package com.augurit.common.system.web.index;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.extmodel.base.ExtElement;
import com.augurit.common.base.extmodel.base.HtmlElement;
import com.augurit.common.system.service.org.IAcMenuService;
import com.augurit.common.system.service.org.IAcUserShortcutService;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.org.form.AcMenuForm;
import com.augurit.common.system.web.org.form.AcUserShortcutForm;
import com.augurit.common.system.web.org.form.AtHomepageConfigForm;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * 系统首页Action类
 */
@SuppressWarnings("serial")
@Results(value={@Result(name="reload", location="index.action", type="redirect")})
public class IndexAction extends ExtBaseAction implements ModelDriven<AcMenuForm>, Preparable{

	// Service 引用
	@Autowired
	private IAcMenuService acMenuService;
	
	@Autowired
	private IAcUserShortcutService acUserShortcutService;
	
	// 属性
	private Long id; 			// Ext节点ID
	private AcMenuForm form;	// 用于查询菜单的Form对象
	
	
	/**
	 * 系统首页的默认入口方法，根据indexForward属性值定位到不同的首页JSP文件
	 */
	public String execute() throws Exception {

		// 获取菜单树数据，保存到Request变量中和设置默认预加载ID值
		List tree = acMenuService.getMenusByMenuLevel(this.getLoginUser(), this.getHomepageConfig());
		this.getRequest().setAttribute(WebContextUtils.REQ_PRELOAD_MENU_STORE, tree);
		setDefaultPreloadNodeId(tree);
		
		// 获取用户自定义快捷菜单数据，保存到Request变量中
		List<AcUserShortcutForm> shortcuts = acUserShortcutService.getShortcuts(this.getLoginUserId());
		this.getRequest().setAttribute(WebContextUtils.REQ_USER_SHORTCUT_STORE, shortcuts);
		
		return this.getHomepageConfig().getHomepageCode();
	}

	/**
	 * 获取指定节点的子节点列表
	 */
	public String getChildren() throws Exception {
		List children = acMenuService.getChildTree(this.getLoginUser(), id, 1, this.getHomepageConfig().getModelName(), true);
		extRenderListJson(children, false);
		return null;
	}

	/**
	 * 获取指定节点的子节点列表，并且添加TotalCount属性JSON字串
	 */
	public String getChildrenAndCount() throws Exception {
		List children = acMenuService.getChildTree(this.getLoginUser(), id, 1, this.getHomepageConfig().getModelName(), true);
		extRenderListJson(children, true);
		return null;
	}
	
	/**
	 * 获取指定节点的子节点列表，并且添加TotalCount属性JSON字串
	 */
	public String searchMenu() throws Exception {
		List<AcMenuForm> menus = acMenuService.searchMenu(this.getLoginUser(), form);
		extRenderListJson(menus, false);
		return null;
	}
	
	/**
	 * 获取指定节点的两层树节点列表
	 */
	public String getAccordionItems() throws Exception {
		List<AcMenuForm> children = acMenuService.getChildTree(this.getLoginUser(), id, 2);
		List<HtmlElement> accordions = OrgUtils.convertToHtmlElementForAccordions(children);
		extRenderListJson(accordions, true);
		return null;
	}
	
	/**
	 * 获取用户自定义的快捷菜单列表
	 */
	public String getShortcuts() throws Exception {
		List<AcUserShortcutForm> shortcuts = acUserShortcutService.getShortcuts(this.getLoginUserId());
		extRenderListJson(shortcuts, true);
		return null;
	}

	/**
	 * 设置默认预加载的节点ID
	 * @param tree
	 */
	private void setDefaultPreloadNodeId(List tree){
		if(tree != null && tree.size() > 0){
			id = Long.valueOf(((ExtElement)tree.get(0)).getId());
		}
	}
	
	/**
	 * 获取系统首页配置信息
	 */
	public AtHomepageConfigForm getHomepageConfig(){
		return WebContextUtils.getAtHomepageConfigBuffer(this.getApplication());
	}
	
	
	public void prepareSearchMenu() throws Exception {
		prepareModel();
	}
	
	public void prepare() throws Exception {
	}
	
	/**
	 * 模型访问方法，使得模型数据自动与页面字段进行关联绑定
	 */
	public AcMenuForm getModel() {
		return form;
	}

	/**
	 * 模型预装载方法，针对新增或编辑数据时预先准备对象数据
	 */
	public void prepareModel() throws Exception {
		form = new AcMenuForm();
	}
	
	
	
	//******************** 基本属性的getter和setter方法 ********************
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
